;; ===== GENERAL STUFF =====

;; Install Marmelade- package archive
;;(require 'package)
;;(add-to-list 'package-archives 
;;    '("marmalade" .
;;      "http://marmalade-repo.org/packages/"))
;;(package-initialize)

;; "y or n" instead of "yes or no"
(fset 'yes-or-no-p 'y-or-n-p)

;; show matching parens
(show-paren-mode t)

;; store backups in ~/.emac.d/backup
(setq backup-directory-alist '(("." . "~/.emacs.d/backup")))

;;syntax highlighing on
(global-font-lock-mode 1)

;;rich syntax highlighting
(setq font-lock-maximum-decoration t) 

;;move 1 line when navigating with C-n, C-p
(setq scroll-step            1
      scroll-conservatively  10000)



;; ===== colors =====

;; same color scheme for tmux & pure terminal
;;(setq frame-background-mode "light")

;; string color
(set-face-foreground 'font-lock-string-face "blue")

;; comment color
(set-face-foreground 'font-lock-comment-face "darkgray")

;; keywords color (color-53 is a bit darker than magenta)
(set-face-foreground 'font-lock-keyword-face "color-53")

;; types color
(set-face-foreground 'font-lock-type-face "color-53")

;; functin name color
(set-face-foreground 'font-lock-function-name-face "color-166")

;; variables color
(set-face-foreground 'font-lock-variable-name-face "black")

;; preprocessor directive color
(set-face-foreground 'font-lock-preprocessor-face "color-52")

;; namespaces
(set-face-foreground 'font-lock-constant-face "cyan")

;; set region background colour
(set-face-background 'region "color-251")

;;color-22 green for future use



;; ===== set intendation =====

;;(add-hook 'c-mode-common-hook '(lambda () (c-toggle-auto-state 1)))



;; ===== set coding style =====

;;    “gnu”: The default style for GNU projects
;;    “k&r”: What Kernighan and Ritchie, the authors of C used in their book
;;    “bsd”: What BSD developers use, aka “Allman style” after Eric Allman.
;;    “whitesmith”: Popularized by the examples that came with Whitesmiths C, an early commercial C compiler.
;;    “stroustrup”: What Stroustrup, the author of C++ used in his book
;;    “ellemtel”: Popular C++ coding standards as defined by “Programming in C++, Rules and Recommendations,” Erik Nyquist and Mats Henricson, Ellemtel
;;    “linux”: What the Linux developers use for kernel development
;;    “python”: What Python developers use for extension modules
;;    “java”: The default style for java-mode (see below)
;;    “user”: When you want to define your own style
(setq c-default-style "linux" c-basic-offset 4)



;; ===== flyspell =====
(dolist (hook '(text-mode-hook))
	(add-hook hook (lambda () (flyspell-mode 1))))
(dolist (hook '(change-log-mode-hook log-edit-mode-hook))
      	(add-hook hook (lambda () (flyspell-mode -1))))



;; ===== #if 0 highlight =====

(defun cpp-highlight-if-0/1 ()
  "Modify the face of text in between #if 0 ... #endif."
  (interactive)
  (setq cpp-known-face '(background-color . "color-254"))
  (setq cpp-unknown-face 'default)
  (setq cpp-face-type 'dark)
  (setq cpp-known-writable 't)
  (setq cpp-unknown-writable 't)
  (setq cpp-edit-list
        '((#("1" 0 1
             (fontified nil))
           nil
           (background-color . "color-254")
           both nil)
          (#("0" 0 1
             (fontified nil))
           (background-color . "color-254")
           nil
           both nil)))
  (cpp-highlight-buffer t))

(defun jpk/c-mode-hook ()
  (cpp-highlight-if-0/1)
  (add-hook 'after-save-hook 'cpp-highlight-if-0/1 'append 'local)
  )

(add-hook 'c-mode-common-hook 'jpk/c-mode-hook)



;; ===== toggle panes =====

(defun toggle-frame-split ()
  "If the frame is split vertically, split it horizontally or vice versa.
Assumes that the frame is only split into two."
  (interactive)
  (unless (= (length (window-list)) 2) (error "Can only toggle a frame split in two"))
  (let ((split-vertically-p (window-combined-p)))
    (delete-window) ; closes current window
    (if split-vertically-p
        (split-window-horizontally)
      (split-window-vertically)) ; gives us a split with the other window twice
    (switch-to-buffer nil))) ; restore the original window in this part of the frame

;; I don't use the default binding of 'C-x 5', so use toggle-frame-split instead
(global-set-key (kbd "C-x 5") 'toggle-frame-split)



;; ===== bindings =====

;; delete line
(global-set-key (kbd "M-9") 'kill-whole-line)

;; copy line
(global-set-key "\C-c\C-k" 'copy-line)


;; resize panes
(global-set-key (kbd "S-C-<left>") 'shrink-window-horizontally)
(global-set-key (kbd "S-C-<right>") 'enlarge-window-horizontally)
(global-set-key (kbd "S-C-<down>") 'shrink-window)
(global-set-key (kbd "S-C-<up>") 'enlarge-window)



;; ===== startup =====

(put 'narrow-to-region 'disabled nil)
(put 'narrow-to-page 'disabled nil)



;; ===== EXTERNAL STUFF =====
;; ===== line numbers by linum =====

(require 'linum)

;; read path
(add-to-list 'load-path "~/.emacs.d/")

;; read linum.el
(load "linum.el")

;; switch on linum-mode on startup
(global-linum-mode 1)

;; 1 space between line numbers and text + solid line
(setq linum-format "%2d \u2502")



;; ===== auto complete =====

(add-to-list 'load-path "~/.emacs.d/auto-complete")
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "/home/kuba/.emacs.d/auto-complete/ac-dict")
(ac-config-default)

;; don't show auto-complete menu automatically
(setq ac-auto-start nil)

;; set binding to pop up auto-complete mentu
(define-key ac-mode-map (kbd "M-/") 'auto-complete)

;; use C-n, C-p to navigate
(setq ac-use-menu-map t)
(define-key ac-menu-map "\C-n" 'ac-next)
(define-key ac-menu-map "\C-p" 'ac-previous)



;; ===== pair braces =====

(add-to-list 'load-path "~/.emacs.d/auto-pair")
(require 'autopair)
(autopair-global-mode)



;; ===== syntax on-the-fly checking by flymake =====

;;(add-to-list 'load-path "~/.emacs.d/emacs-flymake")
;;(require 'flymake)
;;(add-hook 'find-file-hook 'flymake-find-file-hook)



;; ===== project tree by dirtree =====

(add-to-list 'load-path "~/.emacs.d/emacs-dirtree")
(require 'dirtree)



;; ===== sunrise commander =====

;;(add-to-list 'load-path "~/.emacs.d/sunrise-commander")
;;(require 'sunrise-x-tree)



;; ===== window numbers =====

(add-to-list 'load-path "~/.emacs.d/window-number")
(require 'window-number)
(window-number-mode)



;; ===== cedet =====

(add-to-list 'load-path "~/.emacs.d/cedet")
(load-file "~/.emacs.d/cedet-1.1/common/cedet.el")
(global-ede-mode 1)
(semantic-load-enable-minimum-features)
(semantic-load-enable-code-helpers)



;; ===== ecb =====
(setq stack-trace-on-error t)
(add-to-list 'load-path "~/.emacs.d/ecb")
(load-file "~/.emacs.d/ecb/ecb.el")
(require 'ecb)



;; ===== TTY EMACS SPECYFIC STUFF =====
